import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';

export default function setupElementPlus(app) {
  // 注册Element Plus
  app.use(ElementPlus, { size: 'small', zIndex: 3000 });

  // 注册所有Element Plus图标
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
  }

  // 手动注册常用的Element Plus组件（以防自动导入失败）
  const elementComponents = [
    'ElButton',
    'ElInput',
    'ElMenu',
    'ElMenuItem',
    'ElSubMenu',
    'ElCard',
    'ElTable',
    'ElTableColumn',
    'ElPagination',
    'ElDialog',
    'ElForm',
    'ElFormItem',
    'ElSelect',
    'ElOption',
    'ElRadio',
    'ElRadioGroup',
    'ElRadioButton',
    'ElCheckbox',
    'ElCheckboxGroup',
    'ElSwitch',
    'ElSlider',
    'ElDatePicker',
    'ElTimePicker',
    'ElUpload',
    'ElIcon',
    'ElAlert',
    'ElMessage',
    'ElMessageBox',
    'ElNotification',
    'ElLoading',
    'ElContainer',
    'ElHeader',
    'ElAside',
    'ElMain',
    'ElFooter',
    'ElBreadcrumb',
    'ElBreadcrumbItem',
    'ElTag',
    'ElSkeleton',
    'ElDropdown',
    'ElDropdownMenu',
    'ElDropdownItem',
    'ElAvatar',
    'ElLink',
    'ElEmpty',
  ];

  // 从element-plus中导入并注册组件
  elementComponents.forEach(componentName => {
    import(`element-plus`)
      .then(module => {
        const component = module[componentName];
        if (component) {
          app.component(componentName, component);
        }
      })
      .catch(err => {
        console.warn(`Failed to import ${componentName}:`, err);
      });
  });
}
